package org.smr.ministore.controller;

import javax.annotation.Resource;

import org.blade.msg.DataMessage;
import org.blade.msg.Message;
import org.blade.utils.Pager;
import org.smr.ministore.entities.GoodsType;
import org.smr.ministore.entities.vo.Assign2OrgansVO;
import org.smr.ministore.service.GoodsTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;


@Controller("GoodsTypeController")
public class GoodsTypeController {

	@Resource
	private GoodsTypeService service;
	
	/**
	 * 保存编辑后的GoodsType信息
	 * @param GoodsType
	 * @return
	 */
	@ResponseBody
	@RequestMapping(method = RequestMethod.PATCH, value = "/GoodsType/edit")
	public Message modify(@RequestBody GoodsType GoodsType){
		Message msg = new Message();
		
		if(null == GoodsType.getId()){
			msg.setStatus(service.save(GoodsType));
		}else{
			msg.setStatus(service.updateById(GoodsType));
		}
		return msg;
	}
	
	/**
	* 跳转到列表查询GoodsType
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, value = "/GoodsType/list-page")
	public String toList(){
		return "/GoodsType/GoodsType-list";
	}

	/**
	*  根据id查询GoodsType
	* @return
	*/
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "/GoodsType")
	public GoodsType get( Long id ){
		return service.queryById(id);
	}


    /**
	 * 分页查询GoodsType信息
	 * @param GoodsType
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/GoodsType/list")
	public Pager<GoodsType> list(@RequestBody GoodsType GoodsType , long pageSize, long pageNum ){

		return service.paginate(GoodsType, pageSize, pageNum);

	}
	
	
	/**
	 * 根据ID删除GoodsType信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value = "/GoodsType/remove/{id}",method = RequestMethod.DELETE)
	public Message remove(@PathVariable("id") Long id, Long organId){

		GoodsType goodsType = new GoodsType();
		goodsType.setId(id);
		goodsType.setOrganId(organId);
		Message msg = new Message();
		msg.setStatus(service.removeById(goodsType));
		return msg;
	}


	@ResponseBody
	@RequestMapping( method = RequestMethod.POST, value = "/GoodsType/getGoodsTypeTree")
	public DataMessage<List<GoodsType>> getGoodsTypeTree(@RequestBody GoodsType goodsType) {
        DataMessage<List<GoodsType>> message =  DataMessage.success(this.service.getGoodsTypeTree(goodsType));
		return message;
	}

    @ResponseBody
    @RequestMapping( method = RequestMethod.PUT, value = "/GoodsType/assign2Organs")
	public Message assign2Organs(@RequestBody Assign2OrgansVO assign2Organs){
        Message msg = new Message();
        msg.setStatus(this.service.assignToOrgans(assign2Organs));
        return msg;
    }

}
